package com.sdry.web.controller.zc.quality;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcInspectionEntity;
import com.sdry.model.zc.ZcParetsTestEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.model.zc.ZcTestDetailsEntity;
import com.sdry.model.zc.ZcTestEntity;
import com.sdry.service.zc.ZcTestService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcTestController   
 * @Description:  生产配件检验
 * @Author:       zc   
 * @CreateDate:   2018年9月27日 上午9:48:58   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("test")
public class ZcTestController {

	@Resource
	private ZcTestService ztService;
	/**
	 * 进入检验页面
	 * @return
	 */
	@RequestMapping("enterTestPage")
	public String enterTestPage(){
		return "/zc/quality/test";
	}
	/**
	 * 查询报检单列表
	 * @param request
	 * @param response
	 * @param zqc
	 */
	@RequestMapping("selectInspectionList")
	public void selectInspectionList(HttpServletRequest request,HttpServletResponse response){
		List<ZcInspectionEntity> inspectionList = ztService.selectInspectionList();
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(inspectionList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查找报检单
	 * @param request
	 * @param response
	 * @param zqc
	 */
	@RequestMapping("selectInspection")
	public void selectInspection(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zqc){
		String keyword = zqc.getKeyword();
		String code = zqc.getCode();
		if(keyword == null){
			keyword = "";
		}
		if(code == null){
			code = "";
		}
		zqc.setKeyword(keyword);
		zqc.setCode(code);
		
		ZcInspectionEntity inspection = ztService.selectInspection(zqc);
		JSONObject json= JSONObject.fromObject(inspection);  
		try {
			if(inspection!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存检验单
	 * @param request
	 * @param response
	 * @param zcParetsTestEntity
	 * @param httpSession
	 */
	@RequestMapping("insertParetsTest")
	public void insertParetsTest(HttpServletRequest request,HttpServletResponse response,ZcParetsTestEntity zcParetsTestEntity,HttpSession httpSession){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcParetsTestEntity.setTestPerson(userName);
		zcParetsTestEntity.setTestDate(sdf.format(date));
		int insertParetsTest = ztService.insertParetsTest(zcParetsTestEntity);
		try {
			ResponseUtil.write(response, insertParetsTest);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存检验单详情
	 * @param request
	 * @param response
	 * @param zcTestDetailsEntity
	 */
	@RequestMapping("insertTestDetails")
	public void insertTestDetails(HttpServletRequest request,HttpServletResponse response,ZcTestDetailsEntity zcTestDetailsEntity){
		int insertTestDetails = ztService.insertTestDetails(zcTestDetailsEntity);
		try {
			ResponseUtil.write(response, insertTestDetails);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存质检单(待处置单)
	 * @param request
	 * @param response
	 * @param zcTestEntity
	 */
	@RequestMapping("insertTest")
	public void insertTest(HttpServletRequest request,HttpServletResponse response,ZcTestEntity zcTestEntity,HttpSession httpSession){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcTestEntity.settTestPerson(userName);
		zcTestEntity.settTestDate(sdf.format(date));
		int insertTest = ztService.insertTest(zcTestEntity);
		try {
			ResponseUtil.write(response, insertTest);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过日期查找检验记录
	 * @param request
	 * @param response
	 * @param zcTestEntity
	 * @param httpSession
	 */
	@RequestMapping("selectTestLogList")
	public void selectTestLogList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcQueryCriteria.setWorker(userName);
		if(zcQueryCriteria.getKeyword()==null){
			zcQueryCriteria.setKeyword("");
		}
		List<ZcParetsTestEntity> selectTestLogList = ztService.selectTestLogList(zcQueryCriteria);
		int count = ztService.countTestLogList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectTestLogList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过质检单查询检测详情
	 * @param request
	 * @param response
	 * @param ptNumber
	 */
	@RequestMapping("selectTestLogDetailsList")
	public void selectTestLogDetailsList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcTestDetailsEntity> testLogDetailsList = ztService.selectTestLogDetailsList(zcQueryCriteria);
		int count = ztService.countTestLogDetailsList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(testLogDetailsList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
